package com.u2b.business.location;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;



import com.u2b.business.sort.SortProxy;
import com.u2b.global.utils.Constants;
import com.u2b.kvdb.KVDBHandler;


public class DistanceService
{
    private final BaseLoactionService baseLoactionService;
    private final KVDBHandler kvdbHandler;

    public DistanceService()
    {
        baseLoactionService = new BaseLoactionService();
        kvdbHandler = new KVDBHandler();
    }

    /*
     * 
     */
    public void addDistance(HashMap<String, Object> value, HashMap<String, Object> initParams)
    {
        double lat1 = Double.valueOf(initParams.get("latitude").toString());
        double lng1 = Double.valueOf(initParams.get("longitude").toString());
        double lng2 = Double.valueOf(value.get("longitude").toString());
        double lat2 = (Double.valueOf(value.get("latitude").toString()));
        double distance = baseLoactionService.getDistance(lat1, lng1, lat2, lng2);

        value.put("distance", distance);
    }
    public void  addGeoHash(HashMap<String, Object> initParams){
    	
    	double lat=Double.valueOf(initParams.get("latitude").toString());
		double lon=Double.valueOf(initParams.get("longitude").toString());
		String geoHash=Geohash.encode(lat, lon);
		initParams.put("geohash", geoHash);
    	
    }
    public void updateDistanceToKVDB(HashMap<String, Object> initParams)
    {
        String longitude = (String) initParams.get("longtitude");
        String latitude = (String) initParams.get("latitude");
        String value = longitude + ":" + latitude;
        String key = (String) initParams.get("uid");

        kvdbHandler.saveToKvdb(key, value);

    }

    // result每次返回的是20条数据,并且要完成排序.不查询KVDB.如状态列表,活动列表等.应该是按时间排序likegeohash
    public void addDistanceAndSortDistance(List<HashMap<String, Object>> result, HashMap<String, Object> initParams)
    {
        for (HashMap<String, Object> single : result)
        {
            addDistance(single, initParams);
        }
        SortProxy.sortList(result, "distance");
    }

    public void addDistance(List<HashMap<String, Object>> result, HashMap<String, Object> initParams)
    {
        for (HashMap<String, Object> single : result)
        {
            addDistance(single, initParams);
        }
    }

    public void getDistanceFromKVDB(HashMap<String, Object> value)
    {
        String uid = (String) value.get("uid");
        double lat1 = Double.valueOf(value.get("latitude").toString());
        double lng1 = Double.valueOf(value.get("longitude").toString());
        String location = kvdbHandler.getLocationFromKvdb(uid);
        StringTokenizer stringTokenizer = new StringTokenizer(location, ":");
        double lng2 = Double.valueOf(stringTokenizer.nextToken());
        double lat2 = Double.valueOf(stringTokenizer.nextToken());
        double distance = baseLoactionService.getDistance(lat1, lng1, lat2, lng2);

        value.put("distance", distance);

    }
}
